package com.ivanbiz.user;

import com.ivanbiz.generic.GenericDAOImpl;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.ivanbiz.properties.DepartementException;
import com.ivanbiz.properties.PasswordException;
import com.ivanbiz.properties.UserNameException;
import org.hibernate.Query;

/**
 *
 * @author User
 */
public class UserDAOImpl extends GenericDAOImpl implements UserDAO {

    public boolean cekIsEmptyUserId(String userId) throws Exception {
        Query query = getSessionFactory().getCurrentSession().createQuery("from User u where u.userId=:userId");
        query.setString("userId", userId);
        if (query.uniqueResult() != null) {
            return true;
        } else {
            return false;
        }
    }

    public void flushUser() throws Exception {
        getSessionByGeneric().flush();
    }

    public User login(String userName, String password, long departementId) throws Exception {
        Query query = getSessionFactory().getCurrentSession().createQuery("from User u where u.userId=:userId");
        query.setString("userId", userName);
        User user = (User) query.uniqueResult();
        if (user == null) {
            throw new UserNameException();
        }
        if (!user.getPassword().equals(password)) {
            throw new PasswordException();
        }
        if (user.getDepartement().getId() != departementId) {
            throw new DepartementException();
        }
        return user;
    }
}
